import random
from util import *


class greedy_player():
    def __init__(self, turn) -> None:
        self.BOARD_SIZE = 8
        self.PLAYER_NUM = 'none'
        self.COMPUTER_NUM = 'none'
        self.direction = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]
        if turn == 'player':
            self.PLAYER_NUM = 'black'
            self.COMPUTER_NUM = 'white'
        else:
            self.PLAYER_NUM = 'white'
            self.COMPUTER_NUM = 'black'
        self.empty = 'none'
        self.color = self.COMPUTER_NUM

    def NextPosition(self, board):
        computerTile = self.color
        possibleMoves = getValidMoves(board, computerTile)
        random.shuffle(possibleMoves)
        # [x, y]在角上，则优先走，因为角上的不会被再次翻转
        for x, y in possibleMoves:
            if isOnCorner(x, y):
                return [x, y]
        bestScore = -1
        for x, y in possibleMoves:
            dupeBoard = getBoardCopy(board)
            makeMove(dupeBoard, computerTile, x, y)
            # 按照分数选择走法，优先选择翻转后分数最多的走法
            score = getScoreOfBoard(dupeBoard)[computerTile]
            if score > bestScore:
                bestMove = [x, y]
                bestScore = score
        return bestMove